import fs from 'fs';
import path from 'path';
import ejs from 'ejs';

export async function renderTemplateFiles(src, dest, data) {
  // 确保目标目录存在
  if (!fs.existsSync(dest)) {
    fs.mkdirSync(dest, { recursive: true });
  }

  // 读取源目录内容
  const items = fs.readdirSync(src);

  for (const item of items) {
    const srcPath = path.join(src, item);
    const destPath = path.join(dest, item);

    const stat = fs.statSync(srcPath);

    if (stat.isDirectory()) {
      // 递归处理子目录
      await renderTemplateFiles(srcPath, destPath, data);
    } else {
      // 读取模板文件内容
      const templateContent = fs.readFileSync(srcPath, 'utf8');
      // 渲染模板
      const renderedContent = ejs.render(templateContent, data);
      // 写入目标文件
      fs.writeFileSync(destPath, renderedContent);
    }
  }
}